package com.bbcare.plat.redis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbcare.comm.Constants;
import com.bbcare.comm.ToolUtils;
import com.bbcare.followup.plat.store.ibatis.IConfCacheDAO;
import com.bbcare.followup.plat.store.ibatis.IFollowQueueDAO;
import com.bbcare.followup.plat.store.ibatis.IFollowSchemeDAO;
import com.bbcare.plat.redis.service.IConfCacheRedis;
import com.bbcare.redis.client.ShardedJedisClientImpl;

/*
 * @name:         相关配置表（字典表）等信息redis缓存
 * @author:       DemonLee
 * @createTime:   2017.4.24
 * @description:  字典配置表相关信息缓存
 * @modify:
 *
 */

@Service
public class ConfCacheRedis implements IConfCacheRedis {
    private final Log logger = LogFactory.getLog(ConfCacheRedis.class);

    @Resource(name = "redisClient")
    private ShardedJedisClientImpl redisClient;

    public static int cacheFlag = 0;// redis相关数据库表初始化标志

    @Autowired
    private IConfCacheDAO confCacheDao;

    @Autowired
    private IFollowQueueDAO followQueueDao;

    @Autowired
    private IFollowSchemeDAO followSchemeDao;

    private void initFunc() throws Exception {
        redisClient.setDbIndex(Constants.REDIS_DB_INDEX_CONFIG);
        // 字典配置表相关缓存数据使用2号分区
        if (0 == cacheFlag) {
            logger.warn("---init ConfCacheRedis---");
            cacheFlag = 1;

            // 初始化相关表
            initBsStaticParam();
            initFuQueueInfo();
            initFuSchemeInfo();
        }else{
            logger.warn("--- invoking ConfCacheRedis, non init---");
        }
    }

    // bs_static_param
    private void initBsStaticParam() throws Exception {
        Map<String, Object> inMap = new HashMap<String, Object>();
        inMap.put("tableName", Constants.TABLE_BS_STATIC_PARAM);
        List<Map<String, Object>> outList = confCacheDao.selectBsStaticParam(inMap);
        if (null != outList && 0 != outList.size()) {
            Map<String, Object> staticInfo = new HashMap<String, Object>();
            List<String> nameList = new ArrayList<String>();
            List<String> inList = new ArrayList<String>();
            int t = 0;
            for (t = 0; t < outList.size(); t++) {
                staticInfo = outList.get(t);

                inList.clear();
                inList.add(Constants.CONFIG_BS_STATIC_PARAM_PREFIX_PV);
                inList.add((String) staticInfo.get("PARAM_CODE"));
                inList.add((String) staticInfo.get("TYPE"));
                String strbuf = ToolUtils.assembleKey(inList);
                redisClient.set(strbuf, (String) staticInfo.get("PARAM_VALUE"));

                nameList.clear();
                nameList.add(Constants.CONFIG_BS_STATIC_PARAM_PREFIX_PN);
                nameList.add((String) staticInfo.get("PARAM_CODE"));
                nameList.add((String) staticInfo.get("TYPE"));
                strbuf = ToolUtils.assembleKey(nameList);

                redisClient.set(strbuf, (String) staticInfo.get("PARAM_NAME"));
            }
        }
    }

    // fu_queue_info
    private void initFuQueueInfo() throws Exception {
        Map<String, Object> inMap = new HashMap<String, Object>();
        inMap.put("queueStatus", Constants.QUEUE_STATUS_NOMAL);
        List<Map<String, Object>> outList = followQueueDao.selectQueueInfo(inMap);
        if (null != outList && 0 != outList.size()) {
            Map<String, Object> staticInfo = new HashMap<String, Object>();
            List<String> inList = new ArrayList<String>();
            int t = 0;
            for (t = 0; t < outList.size(); t++) {
                staticInfo = outList.get(t);

                // 先存储queueId:queueName对应关系
                inList.clear();
                inList.add(Constants.CONFIG_FU_QUEUE_INFO_PREFIX_QNAME);
                inList.add((String) staticInfo.get("id"));
                String strbuf = ToolUtils.assembleKey(inList);
                redisClient.set(strbuf, (String) staticInfo.get("queue_name"));
            }
        }
    }

    // fu_scheme_info
    private void initFuSchemeInfo() throws Exception {
        Map<String, Object> inMap = new HashMap<String, Object>();
        List<String> schemeStatusList = new ArrayList<String>();
        schemeStatusList.add(Constants.SCHEME_STATUS_DRAFT);
        schemeStatusList.add(Constants.SCHEME_STATUS_NORMAL);
        inMap.put("schemeStatusIn", schemeStatusList);
        List<Map<String, Object>> outList = followSchemeDao.selectSchemeInfo(inMap);
        if (null != outList && 0 != outList.size()) {
            Map<String, Object> staticInfo = new HashMap<String, Object>();
            List<String> inList = new ArrayList<String>();
            int t = 0;
            for (t = 0; t < outList.size(); t++) {
                staticInfo = outList.get(t);

                // 先存储schemeId:schemeName对应关系
                inList.clear();
                inList.add(Constants.CONFIG_FU_SCHEME_INFO_PREFIX_SNAME);
                inList.add((String) staticInfo.get("id"));
                String strbuf = ToolUtils.assembleKey(inList);
                redisClient.set(strbuf, (String) staticInfo.get("scheme_name"));
            }
        }
    }

    public Object get(String key) throws Exception {
        initFunc();
        return redisClient.get(key);
    }
}
